import time

import requests
import json
from setenv import setLotteryDir
from setenv import getFileDir
from setenv import getFileName

from read_mysql_lottery import get_num_periods
from read_mysql_lottery import get_lottery_num_mysql
from read_mysql_lottery import save_mysql_lottery



def fetch_ssqlotto_numbers_mysql(url,num_periods):

    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
        'Accept-Encoding': 'gzip, deflate, sdch',
        'Accept-Language': 'en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4',
    }

    response = requests.get(url, headers=headers)
    setLotteryDir()

    if response.status_code == 200:
        html = response.text
        # print(html)
        # 假设中奖号码在HTML中的某个特定标签内，例如：<div class="lotto-numbers">
        data = json.loads(str(html))
        # print(data)
        # print(data.get('value').get('lastPoolDraw').get('lotteryDrawNum'))
        # print(data.get('value').get('lastPoolDraw').get('lotteryDrawResult'))
        # print(data.get('value').get('lastPoolDraw').get('lotteryDrawTime'))
        # print(data.get('value').get('lastPoolDraw').get('lotteryGameName'))
        # print(data.get('value').get('lastPoolDraw').get('lotteryGameNum'))
        # lotteryDrawNum = data.get('value').get('lastPoolDraw').get('lotteryDrawNum')
        # lotteryDrawResult = data.get('value').get('lastPoolDraw').get('lotteryDrawResult')
        # lotteryDrawTime = data.get('value').get('lastPoolDraw').get('lotteryDrawTime')
        # lotteryGameName = data.get('value').get('lastPoolDraw').get('lotteryGameName')
        # lotteryGameNum = data.get('value').get('lastPoolDraw').get('lotteryGameNum')
        for i in range(0,len(data.get('result'))):
            # print(data.get('result')[i].get('code'))
            # print(data.get('result')[i].get('red'))
            # print(data.get('result')[i].get('blue'))
            # print(data.get('result')[i].get('date').split('('))
            # print(data.get('result')[i].get('name'))
            lotteryDrawNum = data.get('result')[i].get('code')
            lotteryDrawResult = data.get('result')[i].get('red').split(',')
            blue = data.get('result')[i].get('blue')
            lotteryDrawResult.append(blue)
            lotteryDrawTime = data.get('result')[i].get('date').split('(')
            lotteryGameName = data.get('result')[i].get('name')
            if lotteryDrawNum == num_periods :
                sql = "INSERT INTO lottery_ssq (record_date,num_periods,num1,num2,num3,num4,num5,num6,num7) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)"
                values = (lotteryDrawTime[0], lotteryDrawNum, lotteryDrawResult[0], lotteryDrawResult[1], lotteryDrawResult[2], lotteryDrawResult[3], lotteryDrawResult[4], lotteryDrawResult[5], lotteryDrawResult[6])
                save_mysql_lottery(sql,values)

                return lotteryDrawResult
    return []




if __name__ == '__main__':

    num_periods = get_num_periods("ssq",1)
    print(num_periods)
    table_name = "lottery_ssq"
    lottery_num = get_lottery_num_mysql(table_name,num_periods)
    print(lottery_num)
    if len(lottery_num) == 0:
        print("mysql无数据")
        url = 'https://www.cwl.gov.cn/cwl_admin/front/cwlkj/search/kjxx/findDrawNotice?name=ssq&pageNo=1&pageSize=1&systemType=PC'  # 替换为实际的大乐透中奖号码公布页面URL
        # print(url)
        numbers = fetch_lotto_numbers_mysql(url,num_periods)
        if len(numbers) == 0:
            print("num_periods未开奖")
        print(numbers)
